package cn.com.seederland.gkrpc;

import java.lang.reflect.Method;
import java.util.Arrays;

/**
 * @ClassName ServiceDescriptor
 * @Author SighFate
 * @Date 2020/10/06 20:33
 * @Version 1.0
 * @Description 表示服务
 */
public class ServiceDescriptor {
    /** 类名 */
    private String clazz;
    /** 方法名 */
    private String method;
    /** 返回类型 */
    private String returnType;
    /** 参数类型 */
    private String[] parameterTypes;

    public ServiceDescriptor(String clazz, String method, String returnType, String[] parameterTypes) {
        this.clazz = clazz;
        this.method = method;
        this.returnType = returnType;
        this.parameterTypes = parameterTypes;
    }

    public ServiceDescriptor() {
    }

    public static ServiceDescriptor from (Class clazz, Method method) {
        ServiceDescriptor serviceDescriptor = new ServiceDescriptor();
        serviceDescriptor.setClazz(clazz.getName());
        serviceDescriptor.setMethod(method.getName());

        Class[] parameterClasses = method.getParameterTypes();
        String[] parameterTypes = new String[parameterClasses.length];

        for (int i = 0; i < parameterClasses.length; i++) {
            parameterTypes[i] = parameterClasses[i].getName();
        }
        serviceDescriptor.setParameterTypes(parameterTypes);

        return serviceDescriptor;
    }

    /**
     * 获取类名
     * @return 获取类名
     */
    public String getClazz() {
        return clazz;
    }

    /**
     * 设置类名
     * @param clazz
     */
    public void setClazz(String clazz) {
        this.clazz = clazz;
    }

    /**
     * 获取方法名称
     * @return 方法名称
     */
    public String getMethod() {
        return method;
    }


    /**
     * 设置方法名称
     * @param method 方法名称
     */
    public void setMethod(String method) {
        this.method = method;
    }

    /**
     *
     * 获取返回类型
     * @return 返回类型
     */
    public String getReturnType() {
        return returnType;
    }

    /**
     * 设置返回类型
     * @param returnType 返回类型
     */
    public void setReturnType(String returnType) {
        this.returnType = returnType;
    }

    /**
     * 获取参数类型列表
     * @return 参数类型列表
     */
    public String[] getParameterTypes() {
        return parameterTypes;
    }

    /**
     * 设置参数类型列表
     * @param parameterTypes 参数类型列表
     */
    public void setParameterTypes(String[] parameterTypes) {
        this.parameterTypes = parameterTypes;
    }

    @Override
    public String toString() {
        return "ServiceDescriptor{" +
                "clazz='" + clazz + '\'' +
                ", method='" + method + '\'' +
                ", returnType='" + returnType + '\'' +
                ", parameterTypes=" + Arrays.toString(parameterTypes) +
                '}';
    }

    @Override
    public int hashCode() {
        return toString().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return  false;
        }
        ServiceDescriptor that = (ServiceDescriptor)obj;
        return this.toString().equals(that.toString());
    }
}
